package com.hencoder.hencoderpracticedraw4.practice;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.hencoder.hencoderpracticedraw4.R;

/**
 * Created by yx on 2017/8/10.
 */

public class Practice00FlipboardView extends View {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this, "degree", 0, -180);
    Camera camera = new Camera();
    private int degree;
    Bitmap bitmap;

    public Practice00FlipboardView(Context context) {
        super(context);
    }

    public Practice00FlipboardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Practice00FlipboardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    {

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);
        objectAnimator.setDuration(2500);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        float newZ = - displayMetrics.density * 6;
        camera.setLocation(0, 0, newZ);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        objectAnimator.start();
    }


    @SuppressWarnings("unused")
    public void setDegree(int degree) {
        this.degree = degree;
        invalidate();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        objectAnimator.end();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth();
        int y = getHeight();
        int centerX = x / 2;
        int centerY = y / 2;
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();

        canvas.save();

        canvas.clipRect(centerX - bitmapWidth / 2, 0, centerX, y);

        canvas.drawBitmap(bitmap, centerX - bitmapWidth / 2, centerY - bitmapHeight / 2, paint);
        canvas.restore();


        canvas.save();
        if (degree > -90) {
            canvas.clipRect(centerX, 0, centerX + bitmapWidth / 2, y);
        } else {
            canvas.clipRect(centerX - bitmapWidth / 2, 0, centerX, y);
        }
        camera.save();
        camera.rotateY(degree);
        canvas.translate(centerX, centerY);
        camera.applyToCanvas(canvas);
        canvas.translate(-centerX, -centerY);
        camera.restore();

        canvas.drawBitmap(bitmap, centerX - bitmapWidth / 2, centerY - bitmapHeight / 2, paint);

        canvas.restore();


    }
}
